package Getopt::Moose::Meta::Attribute;

use if $ENV{DEBUG} => 'Smart::Comments';

use Moose::Role;

use Carp 'confess';


has 'cmd_name' => (
    is => 'rw',
    isa => 'Str',
    predicate => 'has_cmd_name',
);

has 'cmd_type' => (
    is => 'rw',
    isa => 'Str',
    predicate => 'has_cmd_type',
);

has 'cmd_aliases' => (
    is => 'ro',
    isa => 'ArrayRef[Str]',
    predicate => 'has_cmd_aliases',
);


around '_process_options' => sub {
    ### Getopt::Moose::Meta::Attribute::_process_options() : @_
    my ($next, $self, $name, $options) = (shift, @_);

    my $default = $options->{default};

    # Make it lazy because getopt attribute have to be set earlier
    $options->{lazy} = 1;

    $options->{default} = sub {
        ### Getopt::Moose::Meta::Attribute::$options->{default}->() : $name
        my $self = shift;
        my $getopt = $self->getopt;
        if (defined $getopt->options->{$name}) {
            return $getopt->options->{$name};
        }
	if (defined $default) {
	    return ref $default eq 'CODE' ? $default->(@_) : $default;
	};
        return;
    };

    $next->(@_);
};


1;
